<template>
	<view class="detail">
		<view class="title">
			{{detail.title}}
		</view>
		<view class="info">
			<view class="author">
				编辑：{{detail.author}}
			</view>
			<view class="time">
				发布日期：{{detail.posttime | formatDate}}
			</view>

		</view>
		<view class="content">
			<!-- rich-text 用于解析富文本的组件 -->
			<rich-text :nodes="detail.content"></rich-text>
		</view>
		<view class="description">
			声明：本站的内容均采集与腾讯新闻，如果侵权请联系管理（513894357@qq.com）进行整改删除，本站进行了内容采集不代表本站及作者观点，若有侵犯请及时联系管理员，谢谢您的支持。
		</view>
	</view>
</template>

<script>
	import {
		parseTime
	} from '@/utils/tool.js'
	export default {
		data() {
			return {
				options: null,
				detail: {}
			};
		},
		onLoad(query) {
			this.options = query
			this.getDetail()

		},
		methods: {
			getDetail() {
				uni.request({
					url: 'https://ku.qingnian8.com/dataApi/news/detail.php',
					data: {
						cid: this.options.cid,
						id: this.options.id
					},
					success: (res) => {
						// 修改图片样式
						res.data.content = res.data.content.replace(/<img/ig, '<img style="max-width:100%"')
						this.detail = res.data
						uni.setNavigationBarTitle({
							title: this.detail.title
						})

						// 保存浏览记录到本地缓存
						this.saveHistory()

					}

				})
			},
			saveHistory() {

				let historyArr = uni.getStorageSync('historyArr') || []
				const item = {
					id: this.detail.id,
					cid: this.detail.classid,
					title: this.detail.title,
					picurl: this.detail.picurl,
					looktime: parseTime(Date.now())
				}

				const index = historyArr.findIndex(ele => ele.id === item.id)
				if (index !== -1) {
					// 存在则删除
					historyArr.splice(index, 1)
				}

				historyArr.unshift(item)
				historyArr = historyArr.slice(0, 10)
				uni.setStorageSync('historyArr', historyArr)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding: 30rpx;

		.title {
			font-size: 46rpx;
			color: #333;
		}

		.info {
			display: flex;
			justify-content: space-between;
			margin: 30rpx 0;
			padding: 20rpx;
			background-color: #f6f6f6;

			font-size: 25rpx;
			color: #666;
		}

		.content {
			padding-bottom: 50rpx;


		}

		.description {
			background: #FEF0F0;
			font-size: 26rpx;
			padding: 20rpx;
			color: #F89898;
			line-height: 1.8em;
		}
	}
</style>